// components/login.js
const httpUrl = getApp().globalData.httpUrl;
const app = getApp();
import Dialog from '@vant/weapp/dialog/dialog';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading:true,
    disabled:false,
    select:false,
    title:'',
    use_text:'',
    privacy_text:'',
    one_introduce:'',
    two_introduce:'',
    image:''
  },
  select(){
    this.setData({
      select:true
    })
  },
  privacy(){
    Dialog.alert({
      title: '隐私政策',
      messageAlign:'left',
      message: this.data.privacy_text,
      confirmButtonText:'已阅读'
    }).then(() => {
      // on close
    });
  },
  use(){
    Dialog.alert({
      title: '使用协议',
      messageAlign:'left',
      message: this.data.use_text,
      confirmButtonText:'已阅读'
    }).then(() => {
      // on close
    });
  },
  // 登录
  register(){
    var that = this;
    if (that.data.select) {
      that.setData({
        loading:!that.data.loading,
        disabled:!that.data.disabled,
      },()=>{
        wx.login({
          success: (res) => {
            wx.request({
                url: httpUrl + '/login',
                method:'GET',
                data: {
                  code:res.code
                },
                header: {
                  'content-type': 'application/json' // 默认值
                },
                success (res) {
                  if (res.data.code == 20041) {
                    wx.setStorageSync('Shop_token', res.data.data.token),
                    wx.showToast({
                      title: '登陆成功',
                      duration:1000,
                      success(){
                        that.setData({
                          loading:!that.data.loading,
                          disabled:!that.data.disabled
                        },()=>{
                          wx.switchTab({
                            url: '/pages/index/index',
                          })
                        })
                      }
                    })
                  }else{
                    wx.showToast({
                      mask:true,
                      title: res.data.msg,
                      icon:'none',
                      duration:2000,
                      success(res){
                        that.setData({
                          loading:!that.data.loading,
                          disabled:!that.data.disabled,
                        })
                      }
                    }); 
                  }
                },
                fail(err){
                  wx.showToast({
                    title: '网络错误',
                    icon:'error',
                    duration:1000,
                    success(){
                      that.setData({
                        loading:!that.data.loading,
                        disabled:!that.data.disabled,
                      })
                    }
                  })
                }
            })
          },
          fail(err){
            wx.showToast({
              title: '网络错误,请稍后重试！',
              icon:'none',
              success(){
                that.setData({
                  loading:!that.data.loading,
                  disabled:!that.data.disabled,
                })
              }
            })
          }
        })
      })
    }else{
      wx.showToast({
        title: '请认真阅读并同意《隐私政策》和《使用协议》',
        icon:'none'
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.setNavigationBarTitle({
      title: "用户登录",
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.showToast({
      title: '请先登录',
      icon:'none',
    });
    wx.request({
      url: httpUrl + '/info/public',
      method:'GET',
      success(res){
        if (res.data.code == 20041) {
          that.setData({
            title:res.data.data.title,
            image:res.data.data.image,
            one_introduce:res.data.data.one_introduce,
            two_introduce:res.data.data.two_introduce,
            use_text:res.data.data.use_text,
            privacy_text:res.data.data.privacy_text
          })
        
        }else{
          wx.showToast({
            title: '网络请求错误',
            icon:'none',
          })
        }
        console.log(res);
      },
      fail(err){
        console.log(err);
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})

